package org.elec.mac.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elec.mac.entity.ExecuteResult;
import org.elec.mac.entity.SCategory;
import org.elec.mac.page.PageList;
import org.elec.mac.page.PageProperty;
import org.elec.mac.service.SCategoryService;
import org.elec.mac.utils.JSONUtil;
import org.elec.mac.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class CategoryController extends BaseController {

    @Autowired
    private SCategoryService sCategoryService;

    @RequestMapping(value = "/cate_list.do")
    public String categoryListHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                      @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                      @RequestParam(value = "limit", required = false) String limit,
                                      @RequestParam(value = "page", required = false) String page) throws IOException {
        if("page".equals(action)) {
            PageProperty pp = new PageProperty();
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                this.setPageInfo(request.getCookies(), pp, limit, page);
                PageList<SCategory> lintPageList = sCategoryService.getPageList(pp);
                map.put("success", true);
                map.put("records", lintPageList.getRecords());
                map.put("recordsTotal", lintPageList.getTotalRecords());
                map.put("recordsFiltered", lintPageList.getTotalRecords());
            }
            catch(Exception ex) {
                log.error("分类列表获取错误", ex);
                map.put("success", false);
                map.put("msg", "分类列表获取错误");
            }
            response.getWriter().write(JSONUtil.map2json(map));
            return null;
        }
        return "/catetory/category_list";
    }

    @RequestMapping(value = "/cate_add.do")
    public void categoryAddHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                   @RequestParam(value = "action", required = false, defaultValue = "") String action) throws IOException {
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");
        if("do".equals(action)) {
            try {
                String name = this.getParameter(request, "name");
                String ename = this.getParameter(request, "ename");
                Map<String, Object> param = new HashMap<>();
                param.put("name", name);
                SCategory category = sCategoryService.get(param);
                if(null != category) {
                    result.setSuccess(false);
                    result.setMsg("分类名已存在");
                }
                if(StringUtils.isBlank(name)) {
                    result.setSuccess(false);
                    result.setMsg("分类名不能为空");
                }
                if(result.isSuccess()) {
                    SCategory scategory = new SCategory();
                    scategory.setName(name);
                    scategory.setEname(ename);
                    scategory.setFlag(true);
                    sCategoryService.insert(scategory);
                    log.info("分类新增");
                }
            } catch(Exception ex) {
                log.error("分类新增错误", ex);
                result.setSuccess(false);
                result.setMsg("分类新增错误");
            }
        }
        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }

    @RequestMapping(value = "/cate_edit.do")
    public void categoryEditHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                    @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                    @RequestParam(value = "id", required = false, defaultValue = "") String id) throws IOException {
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");
        if("edit".equals(action)) {
            try {
                Map<String, Object> params = new HashMap<>();
                params.put("id", Utils.parseLong(id, 0L));
                SCategory category = sCategoryService.get(params);
                result.getBody().put("category", category);
                log.info("分类修改开启");
            } catch(Exception ex) {
                log.error("分类修改开启错误", ex);
                result.setSuccess(false);
                result.setMsg("分类修改开启错误");
            }
        }
        if("doEdit".equals(action)) {
            try {
                String name = this.getParameter(request, "name");
                String ename = this.getParameter(request, "ename");
                if(StringUtils.isBlank(name)) {
                    result.setSuccess(false);
                    result.setMsg("分类名不能为空");
                }
                if(result.isSuccess()) {
                    SCategory category = new SCategory();
                    category.setId(Utils.parseLong(id, 0L));
                    category.setName(name);
                    category.setEname(ename);
                    sCategoryService.update(category);
                    log.info("分类修改");
                }
            } catch(Exception ex) {
                log.error("分类修改错误", ex);
                result.setSuccess(false);
                result.setMsg("分类修改错误");
            }
        }
        if("doDelete".equals(action)) {
            try {
                SCategory category = new SCategory();
                category.setId(Utils.parseLong(id, 0L));
                category.setFlag(false);
                sCategoryService.update(category);
                log.info("分类删除");
            } catch(Exception ex) {
                log.error("分类删除错误", ex);
                result.setSuccess(false);
                result.setMsg("分类删除错误");
            }
        }
        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }
}
